<?php

namespace App\Http\Controllers\Backstage\Company\Furniture;

use App\Http\Controllers\BaseController;
use App\Http\Requests\Backstage\Company\Furniture\Category\CreateRequest;
use App\Http\Requests\Backstage\Company\Furniture\Category\UpdateRequest;
use App\Models\Company\Furniture\Category;
use App\Transformers\Company\MaterialTransformer;

class CategoryController extends BaseController
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $page_size = get_request_page();
        if ($page_size) {
            $articles = Category::paginate($page_size);
            return $this->response->paginator($articles, new MaterialTransformer());
        }
        $articles = Category::all();
        return $this->response->collection($articles, new MaterialTransformer());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateRequest $request)
    {
        \DB::transaction(function () use ($request) {
            $attributes = $request->validated();
            $model = Category::create($attributes);
            return $model;
        });
        return $this->response->noContent();
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = Category::find($id);
        return $this->response->item($model, new MaterialTransformer());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        \DB::transaction(function () use ($request, $id) {
            $attributes = $request->validated();
            $category = Category::find($id);
            $model = $category->update($attributes);
            return $model;
        });
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
